/*
 * jQuery Combobox
 * http://jqueryui.com/demos/autocomplete/#combobox
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * Modify by waiting@issence.com
 * http://waiting.javaeye.com/blog/714655
 *
 * Depends:
 *	jquery.ui.widget.js
 *	UI Core, UI Widget, UI Position
 * Update:
 *	2010.07.16	扩展设置, 解决兼容, 双击输入框选中内容 
 *	2010.07.23	修正输入+或者c++时抛出 'invalid quantifier +' 错误
 *	2010.08.04	FF下给input添加input事件，补丁解决中文输入时触发匹配问题
 *				解决通过下拉菜单选择项目之后继续输入字符到不匹配状态后失焦时无法清空
 *
 * Bugs:
 *
 * $(selector).combobox()
 * $(selector).combobox({size: 30})
 * $(selector).combobox().combobox("option", {size: 30, delay: 300})	// greater delay for ajax
 *
 */

/* FIX 下拉超长则纵向滚动条
<style>
.ui-button { margin-left: -1px; }
.ui-button-icon-only .ui-button-text { padding: 0.35em; }
.ui-autocomplete-input {margin: 0; padding: 0.48em 0em 0.47em 0.4em; *padding:0.72em 0 0.46em 0.4em;}
ul.ui-autocomplete {max-height: 280px; overflow-x: hidden; overflow-y: auto; padding:2px;}
</style>
*/

// combobox
if (typeof $.widget === 'function') {
	(function($) {
		$.widget("ui.combobox", {
			options: {
				delay: 0,
				maxSize: 50,
				acbegin: 0,
				onpicked:'',
				onchanged:'',
				onsource:'',
				acwidth:0,
				validate:true
			},
			_create:function(){
				var self = this,
					el = self.element,
					maxSize =  self.options.maxSize,
					delay = self.options.delay,
					acwidth = self.options.acwidth,
					acbegin = self.options.acbegin,
					_onpicked = self.options.onpicked,
					_onchanged = self.options.onchanged,
					_onsource = self.options.onsource,
					_validate = self.options.validate,
					_name = $(el).attr('name'),//combox_hrnum
					_id = $(el).attr('id'),//combox_hrnum_2
					bsingle = 'true' == $(el).attr('single');
				
				var valel = null;
				var textel = null;
				if(bsingle){
					textel = $(el);
					valel = $(el);
				}else{
					if($P.isie()){
						//處理ie $(this).prev()方法不兼容問題
						textel = $(el).parent().find('input[type=text]');
						if(textel.length == 0){
							textel = $(el).prev();
						}
					}else{
						textel = $(el).prev();
					}
					
					
					valel = $(el);
				}
				
				// 设定默认值为<select>选定项目
					textel.autocomplete({
						source: function(request, response) {
							var term = $.trim(request.term);
							var ay = new Array();
							$($P.autodata[_name]).each(function(){
									if (ay.length < maxSize && (this.value || this.value=='') && (term || term == '') && term.length>=acbegin && this.value.toLowerCase().indexOf(term.toLowerCase()) >= 0) {
										ay.push(this);
									}
							});
							
							if((typeof _onsource) == 'function'){
								_onsource.call(this,ay);						
							}
							
							response(ay);
							delete ay;
							delete term;
						},
						width: acwidth,
						delay: delay,
						// FIX FF对combox的change事件兼容性 并且change会有滞后性,用于表单当通过按钮选择项目后直接提交会导致select选项未更新 !
						// change结合select事件确保用户必须通过ENTER或鼠标点击来确定条目
						select: function(event, ui) {
							if(bsingle)
								ui.item['value']=ui.item['realvalue'];
							
							valel.val(ui.item.realvalue);
							var record = {};
							$.extend(record,ui.item);
							record['text']=record['value'];
							record['value']=record['realvalue'];
							delete record['realvalue'];
							
							valel.trigger('picked',record);//form table edit event
//							$(this).trigger('picked',record);
							
//							$("#"+_id).trigger('onpicked',record);
							$(this).trigger('onpicked',record);//for jump event
							$(this).data('record',record);
							
							if((typeof _onpicked) == 'function'){
								_onpicked.call(this,record);
							}
						},
						change: function(event, ui) {	// 用select事件来联动select,change只用来做输入不匹配时处理
							if((typeof _onchanged) == 'function'){
								_onchanged.call(this,ui.item);
							}
							
							if(_validate){
								var input = $(this);
								if (!ui.item) {	// 下当选择提示条目后继续输入到不匹配状态然后失焦,IE为空,FF为真
									// remove invalid value, as it didn't match anything
									input.val('');
									valel.val('');
									input.removeData('record');
									return false;
								}
								else {		// ui.item==true || FF 
									if (ui.item.value !== input.val()) {
										input.val('');
										valel.val('');
										input.removeData('record');
									}
								}
							}
						},
						minLength: acbegin
					});
				
				// FF下绑定input事件来兼容输入法中文输入
				if (jQuery.browser.mozilla) {
					textel[0].addEventListener(
							'input', 
							function() {
								var val = this.value;
								if (val) {
									$(this).autocomplete("search", val);
								}
							}, false
					);
				}
			}
		});
	})(jQuery);
}
// ui-autocomplete-input ui-widget ui-widget-content ui-corner-left ui-autocomplete-loading